<template>
	<view class="bank-card">
		<common-list :isDataEmpty="tableData.length === 0" :status="status">
			<template #list>
				<view>
					<view class="list">
						<view class="card" v-for="(item, index) in tableData" :key="index"
							@click="handChoose(item, index)">
							<image class="card-bg"
								src="https://wuyeshop.oss-cn-beijing.aliyuncs.com/image/20230712/20230712103042_k49p.png"
								mode=""></image>
							<view class="card-con">
								<view class="name">{{ item.openingBank }}</view>
								<view class="type">{{ item.name }}</view>
								<view class="number">
									{{ item.newBankCard }}
								</view>
							</view>
							<image class="del"
								src="https://wuyeshop.oss-cn-beijing.aliyuncs.com/image/20230712/20230712103628_6eb6.png"
								mode="" @click.stop="handDel(index)"></image>
						</view>
					</view>
				</view>
			</template>
			<template #empty>
				<view>
					<view class="empty flex flex-direction align-center justify-center">
						<image src="../../static/bankEmpty.png" mode=""></image>
						<text>暂无银行卡</text>
					</view>
				</view>
			</template>
		</common-list>

		<view class="add flex justify-center">
			<button class="cu-btn btn" @click="handAdd">
				<u-icon name="plus"></u-icon>
				<text class="btn-name">添加银行卡</text>
			</button>
		</view>
		<uv-modal ref="modal" show-cancel-button confirm-color="#FF9113" @confirm="handConfirm" width="550rpx">
			<view style="line-height: 100rpx;">确认要删除吗?</view>
		</uv-modal>
	</view>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance,
		inject,
		toRaw
	} from 'vue'
	import commonList from "@/components/commonList/list.vue"
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		doGetBank,
		doDel
	} from '@/apis/bank.js'
	const {
		proxy
	} = getCurrentInstance();
	const $back = inject('$back')


	const modal = ref()
	let oldIndex = 0
	const handDel = (index) => {
		oldIndex = index
		modal.value.open()
	}
	const handConfirm = async () => {
		await doDel(tableData.value[oldIndex].id)
		tableData.value.splice(oldIndex, 1)
	}


	let type = ''
	const handChoose = (e, index) => {
		const eventChannel = proxy.getOpenerEventChannel()
		if (type === 'choose') {
			eventChannel.emit('someEvent', toRaw(e));
			$back()
		} else {
			uni.navigateTo({
				url: '/pagesA/bankCard/add',
				events: {
					successEvent: (info) => {
						tableData.value[index] = info
					}
				},
				success: (res) => {
					res.eventChannel.emit('editData', uni.$uv.deepClone(e))
				}
			})
		}
	}

	const handAdd = () => {
		uni.navigateTo({
			url: '/pagesA/bankCard/add',
			events: {
				successEvent: () => {
					fenctData()
				}
			}
		})
	}

	const tableData = ref([])
	const status = ref('loading')
	const fenctData = async () => {
		status.value = 'loading'
		const data = await doGetBank()
		tableData.value = data.map(el => {
			el.newBankCard = maskCardNumber(el.bankCard)
			return el
		})
		status.value = 'nomore'
	}
	const maskCardNumber = (cardNumber) => {
		const maskedNumber = cardNumber.replace(/\d(?=\d{4})/g, '*');
		return maskedNumber;
	}

	onLoad((e) => {
		if (e?.type) {
			type = e?.type
		}
		fenctData()
	})
</script>

<style lang="scss" scoped>
	.bank-card {
		min-height: 100vh;
		background-color: #fff;
		padding-bottom: 180rpx;

		.header {
			display: flex;
			flex-direction: column;
			position: fixed;
			top: 0;
			width: 750rpx;
			z-index: 5;
			padding-bottom: 30rpx;

			.bg {
				position: absolute;
				top: 0;
				left: 0;
				z-index: 1;
				width: 750rpx;
			}


			&-main {
				flex: 1;
				display: flex;
				align-items: center;
				padding-left: 20rpx;
				position: relative;
				z-index: 2;

				.back {
					image {
						width: 18rpx;
						height: 33rpx;
					}
				}

				.title {
					flex: 1;
					text-align: center;
					font-size: 36rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #333333;
				}
			}
		}

		.list {
			padding: 32rpx;

			.card {
				position: relative;
				height: 267rpx;

				&-bg {
					position: absolute;
					top: 0;
					left: 0;
					z-index: 1;
					width: 686rpx;
					height: 267rpx;
				}

				.del {
					z-index: 3;
					position: absolute;
					right: 30rpx;
					top: 30rpx;
					width: 34rpx;
					height: 34rpx;
				}

				&-con {
					position: relative;
					z-index: 2;
					box-sizing: border-box;
					padding: 26rpx 34rpx;

					.name {
						font-size: 36rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
					}

					.type {
						margin-top: 8rpx;
						font-size: 28rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #FFFFFF;
					}

					.number {
						margin-top: 46rpx;
						font-size: 44rpx;
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #FFFFFF;
					}
				}
			}
		}

		.empty {
			padding-top: 20vh;

			image {
				width: 322.07rpx;
				height: 206.49rpx;
			}

			text {
				margin-top: 24rpx;
				font-size: 26rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
			}
		}

		.add {
			position: fixed;
			bottom: 80rpx;
			width: 750rpx;

			.btn {
				width: 684rpx;
				font-size: 32rpx;
				height: 88rpx;
				background: #FFC434;
				border-radius: 16rpx;
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #333333;

				&::after {
					border: none;
				}
			}
		}
	}
</style>